package com.mentaleye.biz.impl;

import com.mentaleye.bean.GroupPersonBean;
import com.mentaleye.biz.GroupPersonBiz;
import com.mentaleye.dal.model.GroupPersonKey;
import com.mentaleye.service.GroupPersonService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Rock on 2017/5/10.
 */
@Service
public class GroupPersonBizImpl implements GroupPersonBiz{

    private static Logger logger = Logger.getLogger(GroupPersonBiz.class);

    @Autowired private GroupPersonService groupPersonService;

    public void queryByCommpage(CommonPage commonPage){
        groupPersonService.queryByCommpage(commonPage);
    }

    public CommonResult edit(GroupPersonBean bean){
        CommonResult result = new CommonResult();
        boolean success = false;
        try{
            success = groupPersonService.updateByPersonId(bean);
            result.setIsSuccess(success);
        }catch(Exception e){
            logger.error("根据人员编号编辑分组人员异常",e);
        }
        return result;
    }

    public CommonResult add(GroupPersonBean bean){
        CommonResult result = new CommonResult();
        boolean success = false;
        try{
            success = groupPersonService.add(bean);
            result.setIsSuccess(success);
        }catch(Exception e){
            logger.error("添加分组人员异常",e);
        }
        return result;
    }

    public CommonResult removeList(List<Integer> personIdList){
        CommonResult result = new CommonResult();
        boolean success = false;
        try{
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("personIdList",personIdList);
            success = groupPersonService.removeList(map);
            result.setIsSuccess(success);
        }catch (Exception e){
            logger.error("批量删除分组人员异常",e);
        }
        return result;
    }

    public GroupPersonBean queryByKey(Integer groupId, Integer personId){

        try{
            GroupPersonKey key = new GroupPersonKey();
            key.setGroupId(groupId);
            key.setPersonId(personId);
            return groupPersonService.query(key);
        }catch (Exception e){
            logger.error("查询分组人员异常",e);
        }
        return null;
    }

    public Map<String, Object> queryByPersonId(Integer personId){
        try{
            Map<String ,Object> map = new HashMap<String, Object>();
            map.put("id",personId);
            List<Map<String, Object>> personList = groupPersonService.queryByMap(map);
            return personList.get(0);
        }catch (Exception e){
            logger.error("通过人员编号查询分组人员异常",e);
        }
        return null;
    }

    public List<Map<String, Object>> queryNotInClass(Integer classId){
        try{
            return groupPersonService.queryByNotInClass(classId);
        }catch (Exception e){
            logger.error("查询未分组人员异常",e);
        }
        return null;
    }

    public List<Map<String, Object>> queryPersonList(Integer classId,boolean have){
        try{
            return groupPersonService.queryPersonList(classId,have);
        }catch (Exception e){
            logger.error("查询已/未分组人员异常",e);
        }
        return null;
    }
}
